//
//  ChooseCameraAlbumView.swift
//  选择相机相册
//
//  Created by 千策万划-mini on 2025/5/3.
//

import Foundation

class ChooseCameraAlbumView: UIView {
    
    var blockAlbumClick:(() -> Void)?
    var blockCameraClick:(() -> Void)?
    
    // MARK: - lazy
    lazy var imageBack: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(355), W(279))
        result.image = UIImage(named: "icon_choose_album_bg")
        return result
    }()
    lazy var btnClose: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(40+16*2, 40+30*2)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Choose",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 24),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var imageContent: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(275), W(140))
        result.image = UIImage(named: "icon_login_input_bg")
        return result
    }()
    // 相机
    lazy var btnCamera: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(112), W(104))
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        result.setBackgroundImage(UIImage(named: "icon_choose_photo_item_bg"), for: .normal)
        return result
    }()
    lazy var imageCamera: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(36), W(31))
        result.image = UIImage(named: "icon_choose_camera")
        return result
    }()
    lazy var labelCamera: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 14)
        result.fitTitle("Photo Album", variable: 0)
        return result
    }()
    
    // 相册
    lazy var btnAlbum: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(112), W(104))
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        result.setBackgroundImage(UIImage(named: "icon_choose_photo_item_bg"), for: .normal)
        return result
    }()
    lazy var imageAlbum: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(36), W(31))
        result.image = UIImage(named: "icon_choose_album")
        return result
    }()
    lazy var labelAlbum: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 14)
        result.fitTitle("Photograph", variable: 0)
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.backgroundColor = UIColor(hex: "000000", alpha: 0.8)
        
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageBack)
        imageBack.addSubview(labelTitle)
        imageBack.addSubview(imageContent)
        imageBack.addSubview(btnClose)
        
        imageContent.addSubview(btnCamera)
        btnCamera.addSubview(imageCamera)
        btnCamera.addSubview(labelCamera)
        
        imageContent.addSubview(btnAlbum)
        btnAlbum.addSubview(imageAlbum)
        btnAlbum.addSubview(labelAlbum)
        
        resetView()
    }
    
    // MARK: 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            hide()
            break
        case 2: // 相机
            if let block = blockCameraClick {
                block()
            }
            break
        case 3: // 相册
            if let block = blockAlbumClick {
                block()
            }
            break
        default:
            break
        }
    }
    
    // 显示
    func show() {
        if let window = WindowManager.shared.getCurrentWindow() {
            window.endEditing(true)
            // 设置初始状态（比如从底部开始）
            alpha = 0
            imageBack.transform = CGAffineTransform(translationX: 0, y: 50)
            window.addSubview(self)
            // 添加弹出动画
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
                self.alpha = 1
                self.imageBack.transform = .identity
            }
        }
    }

    // 隐藏
    func hide() {
        // 添加消失动画
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn) {
            self.alpha = 0
            self.imageBack.transform = CGAffineTransform(translationX: 0, y: 50)
        } completion: { _ in
            self.removeFromSuperview()
            // 重置 transform，以便下次显示
            self.imageBack.transform = .identity
        }
    }
    
    // MARK: - 重置界面
    func resetView() {
        imageBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-W(20))
        btnClose.rightTop = XY(imageBack.width, 0)
        labelTitle.centerXTop = XY(imageBack.width/2, W(26))
        imageContent.centerXTop = XY(imageBack.width/2, W(112))
        btnCamera.leftCenterY = XY((imageContent.width-btnCamera.width*2)/3, imageContent.height/2)
        btnAlbum.rightCenterY = XY(imageContent.width-btnCamera.left, imageContent.height/2)
        
        imageCamera.centerXTop = XY(btnCamera.width/2, W(25))
        labelCamera.centerXTop = XY(btnCamera.width/2, imageCamera.bottom+W(13))
        
        imageAlbum.centerXTop = XY(btnAlbum.width/2, imageCamera.top)
        labelAlbum.centerXCenterY = XY(btnAlbum.width/2, labelCamera.centerY)
    }
}
